package com.mz.ozy.activity.me;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mz.ozy.R;
import com.mz.ozy.adapter.VipRecordAdapter;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.EventBean;
import com.mz.ozy.bean.VipRecordBean;
import com.mz.ozy.bean.VipUpBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.mvp.presenter.VipRecordPresenter;
import com.mz.ozy.mvp.view.VipRecordContract;
import com.mz.ozy.utils.PayManager;
import com.mz.ozy.utils.ToastUtils;
import com.mz.ozy.widget.dialog.PaySelectDialog;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class VipUpgradeRecordActivity extends BaseActivity<VipRecordContract.View, VipRecordPresenter> implements VipRecordContract.View {

    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    private int pageNo = 1;
    private int tag = 0;
    private List<VipRecordBean.PageBean.PageItemsBean> datas = new ArrayList<>();
    private VipRecordAdapter recordAdapter;
    private int payType = 1;

    private PayManager mPayManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vip_upgrade_record);
        ButterKnife.bind(this);
        initTitle();
        tvCenterTitle.setText("升级记录");
        mPayManager = new PayManager(this);
        EventBus.getDefault().register(this);
        recordAdapter = new VipRecordAdapter(datas);
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        recyclerview.setAdapter(recordAdapter);
        smartRefreshLayout.setEnableAutoLoadmore(true);
        smartRefreshLayout.autoRefresh();
        smartRefreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                pageNo = pageNo + 1;
                tag = 1;
                mPresenter.getList(VipUpgradeRecordActivity.this, String.valueOf(pageNo));
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                pageNo = 1;
                tag = 0;
                mPresenter.getList(VipUpgradeRecordActivity.this, String.valueOf(pageNo));

            }
        });
        recordAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.bnt_cancle:
                        mPresenter.cancleOrder(VipUpgradeRecordActivity.this, datas.get(position).getTesq());
                        break;
                    case R.id.bnt_pay:
                        payType = datas.get(position).getPay_type();
                        mPresenter.onPayOrder(VipUpgradeRecordActivity.this, datas.get(position).getTesq());

                        break;
                }
            }
        });


    }

    @Override
    public void getListSuccess(VipRecordBean bean) {
        if (tag == 0) {
            smartRefreshLayout.setLoadmoreFinished(false);
            smartRefreshLayout.finishRefresh();
            datas.clear();
            if (bean.getPage().getPageItems().size() == 0) {
                llNoData.setVisibility(View.VISIBLE);
            } else {
                llNoData.setVisibility(View.GONE);
            }
        } else if (tag == 1) {
            smartRefreshLayout.finishLoadmore();
        }
        datas.addAll(bean.getPage().getPageItems());
        recordAdapter.setNewData(datas);
        if (bean.getPage().getPageItems().size() == 0 || bean.getPage().getPageSize() * bean.getPage().getPageNumber() >= bean.getPage().getPageTotle()) {
            smartRefreshLayout.setLoadmoreFinished(true);//将不会再次触发加载更多事件
        }
    }

    @Override
    public void cancleOrderSuccess(JSONObject json) {
        ToastUtils.showToast("取消成功");
        smartRefreshLayout.autoRefresh();

    }

    @Override
    public void onPayOrderSuccess(JSONObject json) {

        if (payType == 1) {
            mPayManager.handleAliPay(json.optString("zfb"));

        } else {
            mPayManager.handleWxPay(json);
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(EventBean event) {
        if ((event.getCode() == Constants.EventCode.WX_PAY_SUCCESS || event.getCode() == Constants.EventCode.ALIPAY_PAY_SUCCESS)) {
            ToastUtils.showToast("支付成功");
            smartRefreshLayout.autoRefresh();
        }

    }


}